<?php

Globals::requireClass('Controller');
Globals::requireTable('Carteam');
Globals::requireTable('Driver');
Globals::requireTable('Car');

class carserviceController extends Controller {

    protected $CarteamTable;
    protected $DriverTable;
    protected $CarTable;
    public static $defaultConfig = array(
        'viewEnabled' => true,
        'layoutEnabled' => true,
        'title' => null
    );

    public function __construct($config = null) {
        parent::__construct($config);
        $this->CarteamTable = new CarteamTable($config);
        $this->DriverTable = new DriverTable($config);
        $this->CarTable = new CarTable($config);
    }

    public function carAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        
        if (!in_array(11, $_SESSION['userinfo']['acl'])) {
            $this->redirect("./?m=index&a=main");
            exit;
        }
        $name = $this->getParam('name');
        $status = $this->getParam('status');
        if ($status > 0) {
            $where[] = "`status` = $status";
        } else {
            $where[] = "`status` > 0";
        }
        if (isset($name)) {
            $where[] = "`name` like '%{$name}%'";
        }
        $count = $this->CarteamTable->listCount($where);
        $pageId = $this->getIntParam('page');
        $pageSize = 15;
        $this->view->paging = $this->getPaging($count, $pageSize, $pageId);
        $data = $this->CarteamTable->listPage($where, "id desc", $pageSize, $pageId);
        $this->view->name = $name;
        $this->view->status = $status;
        $this->view->data = isset($data) ? $data : array();
    }

    public function caraddAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        
        $name = $this->getParam('name');
        $nickname = $this->getParam('nickname');
        $code = $this->getParam('code');
        $address = $this->getParam('address');
        $contact = $this->getParam('contact');
        $telephone = $this->getParam('telephone');
        $fax = $this->getParam('fax');
        $email = $this->getParam('email');
        $tax = $this->getParam('tax');
        $remark = $this->getParam('remark');
        $unique = $this->CarteamTable->getRow("`name` = '{$name}' AND `status` > 0");
        if ($unique) {
            echo -1;
            exit;
        }
        $arr = array(
            '`name`' => $name,
            '`nickname`' => $nickname,
            '`code`' => $code,
            '`address`' => $address,
            '`contact`' => $contact,
            '`telephone`' => $telephone,
            '`fax`' => $fax,
            '`email`' => $email,
            '`tax`' => $tax,
            '`remark`' => $remark,
            '`user_id`' => $_SESSION['userinfo']['id'],
            '`createtime`' => date("Y-m-d H:i:s")
        );
        $ID = $this->CarteamTable->add($arr, true);
        echo $ID;
        exit;
    }

    public function carjsonAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        
        $id = $this->getIntParam('id');
        $data = $this->CarteamTable->getRow($id);
        echo json_encode($data);
        exit;
    }

    public function cardormancyAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        
        $id = $this->getIntParam('id');
        $this->CarteamTable->update(array("`status`" => 2), $id);
        echo $id;
        exit;
    }

    public function caractivationAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        
        $id = $this->getIntParam('id');
        $this->CarteamTable->update(array("`status`" => 1), $id);
        echo $id;
        exit;
    }

    public function carupdateAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        
        $id = $this->getIntParam('id');
        $name = $this->getParam('name');
        $nickname = $this->getParam('nickname');
        $code = $this->getParam('code');
        $address = $this->getParam('address');
        $contact = $this->getParam('contact');
        $telephone = $this->getParam('telephone');
        $fax = $this->getParam('fax');
        $email = $this->getParam('email');
        $tax = $this->getParam('tax');
        $remark = $this->getParam('remark');
        $slim = $this->CarteamTable->getRow($id);
        $unique = $this->CarteamTable->getRow("`name` = '{$name}' AND `status` > 0");
        if ($unique && $slim['name'] != $name) {
            echo -1;
            exit;
        }
        $data = array(
            '`name`' => $name,
            '`nickname`' => $nickname,
            '`code`' => $code,
            '`address`' => $address,
            '`contact`' => $contact,
            '`telephone`' => $telephone,
            '`fax`' => $fax,
            '`email`' => $email,
            '`tax`' => $tax,
            '`remark`' => $remark,
        );
        $this->CarteamTable->update($data, $id);
        echo $id;
        exit;
    }

    public function cardeleteAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        
        $id = $this->getIntParam('id');
        $this->CarteamTable->update(array("`status`" => 0), $id);
        echo $id;
        exit;
    }

    /*     * ***********司机************* */

    public function driverAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        
        if (!in_array(12, $_SESSION['userinfo']['acl'])) {
            $this->redirect("./?m=index&a=main");
            exit;
        }
        $name = $this->getParam('name');
        $status = $this->getParam('status');
        if ($status > 0) {
            $where[] = "`status` = $status";
        } else {
            $where[] = "`status` > 0";
        }
        if (isset($name)) {
            $where[] = "`name` like '%{$name}%'";
        }
        $count = $this->DriverTable->listCount($where);
        $pageId = $this->getIntParam('page');
        $pageSize = 15;
        $this->view->paging = $this->getPaging($count, $pageSize, $pageId);
        $data = $this->DriverTable->listPage($where, "id desc", $pageSize, $pageId);
        $this->view->name = $name;
        $this->view->status = $status;
        $this->view->data = isset($data) ? $data : array();
    }

    public function driveraddAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        
        $name = $this->getParam('name');
        $code = $this->getParam('code');
        $telephone = $this->getParam('telephone');
        $cellphone = $this->getParam('cellphone');
        $fax = $this->getParam('fax');
        $email = $this->getParam('email');
        $id_card = $this->getParam('id_card');
        $driver_license = $this->getParam('driver_license');
        $homeless = $this->getParam('homeless');
        $address = $this->getParam('address');
        $wages = $this->getParam('wages');
        $remark = $this->getParam('remark');
        $unqiue1 = $this->DriverTable->getRow("`name` = '{$name}' AND `status` > 0");
        $unqiue2 = $this->DriverTable->getRow("`id_card` = '{$id_card}' AND `status` > 0");
        $unqiue3 = $this->DriverTable->getRow("`driver_license` = '{$driver_license}' AND `status` > 0");
        if ($unqiue1 || $unqiue2 || $unqiue3) {
            echo -1;
            exit;
        }
        $arr = array(
            "`name`" => $name,
            "`code`" => $code,
            "`telephone`" => $telephone,
            "`cellphone`" => $cellphone,
            "`fax`" => $fax,
            "`email`" => $email,
            "`id_card`" => $id_card,
            "`driver_license`" => $driver_license,
            "`homeless`" => $homeless,
            "`address`" => $address,
            "`wages`" => $wages,
            "`remark`" => $remark,
            "`user_id`" => $_SESSION['userinfo']['id'],
            "`createtime`" => date("Y-m-d H:i:s")
        );
        $ID = $this->DriverTable->add($arr, true);
        echo $ID;
        exit;
    }

    public function driverjsonAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        
        $id = $this->getIntParam('id');
        $data = $this->DriverTable->getRow($id);
        echo json_encode($data);
        exit;
    }

    public function driverupdateAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        
        $id = $this->getIntParam('id');
        $name = $this->getParam('name');
        $code = $this->getParam('code');
        $telephone = $this->getParam('telephone');
        $cellphone = $this->getParam('cellphone');
        $fax = $this->getParam('fax');
        $email = $this->getParam('email');
        $id_card = $this->getParam('id_card');
        $driver_license = $this->getParam('driver_license');
        $homeless = $this->getParam('homeless');
        $address = $this->getParam('address');
        $wages = $this->getParam('wages');
        $remark = $this->getParam('remark');
        $unqiue1 = $this->DriverTable->getRow("`name` = '{$name}' AND `status` > 0");
        $unqiue2 = $this->DriverTable->getRow("`id_card` = '{$id_card}' AND `status` > 0");
        $unqiue3 = $this->DriverTable->getRow("`driver_license` = '{$driver_license}' AND `status` > 0");
        $slim = $this->DriverTable->getRow($id);
        if (($unqiue1 && $slim['name'] != $name ) || ($unqiue2 && $slim['id_card'] != $id_card ) || ($unqiue3 && $slim['driver_license'] != $driver_license)) {
            echo -1;
            exit;
        }
        $arr = array(
            "`name`" => $name,
            "`code`" => $code,
            "`telephone`" => $telephone,
            "`cellphone`" => $cellphone,
            "`fax`" => $fax,
            "`email`" => $email,
            "`id_card`" => $id_card,
            "`driver_license`" => $driver_license,
            "`homeless`" => $homeless,
            "`address`" => $address,
            "`wages`" => $wages,
            "`remark`" => $remark
        );
        $this->DriverTable->update($arr, $id);
        echo $id;
        exit;
    }

    public function driverdeleteAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        
        $id = $this->getIntParam('id');
        $this->DriverTable->update(array("`status`" => 0), $id);
        echo $id;
        exit;
    }

    public function driverdormancyAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        
        $id = $this->getIntParam('id');
        $this->DriverTable->update(array("`status`" => 2), $id);
        echo $id;
        exit;
    }

    public function driveractivationAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        
        $id = $this->getIntParam('id');
        $this->DriverTable->update(array("`status`" => 1), $id);
        echo $id;
        exit;
    }

    /*     * ************************ */

    public function vehicleAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        
        if (!in_array(13, $_SESSION['userinfo']['acl'])) {
            $this->redirect("./?m=index&a=main");
            exit;
        }
        $name = $this->getParam('name');
        $status = $this->getParam('status');
        $team_id = $this->getParam('team_id');
        if ($status > 0) {
            $where[] = "`status` = $status";
        } else {
            $where[] = "`status` > 0";
        }
        if (isset($name)) {
            $where[] = "`name` like '%{$name}%'";
        }
        if (isset($team_id)) {
            $where[] = "`team_id` like '%{$team_id}%'";
        }
        $count = $this->CarTable->listCount($where);
        $pageId = $this->getIntParam('page');
        $pageSize = 15;
        $this->view->paging = $this->getPaging($count, $pageSize, $pageId);
        $data = $this->CarTable->listPage($where, "id desc", $pageSize, $pageId);
        $ids = array();
        $tds = array();
        if (count($data)) {
            foreach ($data as $k => $v) {
                $ids[] = $v['driver_id'];
                $tds[] = $v['team_id'];
            }
        }
        $ids = array_unique($ids);
        $tds = array_unique($tds);
        $tstring = implode(",", $tds);
        $string = implode(",", $ids);
        if (!empty($string)) {
            $driver_data = $this->DriverTable->listAll("`id` in ($string)");
            if (count($driver_data)) {
                foreach ($driver_data as $k => $v) {
                    $driver_info[$v['id']] = $v;
                }
                $this->view->driver_info = $driver_info;
            }
        }
        if (!empty($tstring)) {
            $team_data = $this->CarteamTable->listAll("`id` in ($tstring)");
            if (count($team_data)) {
                foreach ($team_data as $k => $v) {
                    $team_info[$v['id']] = $v['name'];
                }
                $this->view->team_info = $team_info;
            }
        }
        //  var_dump($driver_info);exit;
        $team = $this->CarteamTable->listAll("`status` = 1");
        $driver = $this->DriverTable->listAll("`status` = 1");
        $this->view->name = $name;
        $this->view->status = $status;
        $this->view->team_id = $team_id;
        $this->view->team = isset($team) ? $team : array();
        $this->view->driver = isset($driver) ? $driver : array();
        $this->view->data = isset($data) ? $data : array();
    }

    public function vehicleaddAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        
        $name = $this->getParam('name');
        $team_id = $this->getParam('team_id');
        $code = $this->getParam('code');
        $mode = $this->getParam('mode');
        $driver_id = $this->getParam('driver_id');
        $engineer = $this->getParam('engineer');
        $head_weight = $this->getParam('head_weight');
        $load_weight = $this->getParam('load_weight');
        $ciq_code = $this->getParam('ciq_code');
        $clearance_card = $this->getParam('clearance_card');
        $ic_card = $this->getParam('ic_card');
        $electronics_clock = $this->getParam('electronics_clock');
        $bussiness_mode = $this->getParam('bussiness_mode');
        $driving_license_code = $this->getParam('driving_license_code');
        $driving_license_date = $this->getParam('driving_license_date');
        $insurance_force_date = $this->getParam('insurance_force_date');
        $insurance_force_code = $this->getParam('insurance_force_code');
        $maintain_code = $this->getParam('maintain_code');
        $maintain_date = $this->getParam('maintain_date');
        $insurance_business_code = $this->getParam('insurance_business_code');
        $insurance_business_date = $this->getParam('insurance_business_date');
        $taxi_license_code = $this->getParam('taxi_license_code');
        $taxi_license_date = $this->getParam('taxi_license_date');
        $remind_id = $this->getParam('remind_id');
        $remind_day = $this->getParam('remind_day');
        $remark = $this->getParam('remark');
        $unqiue = $this->CarTable->getRow("`name` = '{$name}' AND `status` > 0");
        if ($unqiue) {
            echo -1;
            exit;
        }
        $arr = array(
            "`name`" => $name,
            "`team_id`" => $team_id,
            "`mode`" => $mode,
            "`driver_id`" => $driver_id,
            "`engineer`" => $engineer,
            "`head_weight`" => $head_weight,
            "`load_weight`" => $load_weight,
            "`ciq_code`" => $ciq_code,
            "`clearance_card`" => $clearance_card,
            "`ic_card`" => $ic_card,
            "`electronics_clock`" => $electronics_clock,
            "`bussiness_mode`" => $bussiness_mode,
            "`driving_license_code`" => $driving_license_code,
            "`driving_license_date`" => $driving_license_date,
            "`insurance_force_date`" => $insurance_force_date,
            "`insurance_force_code`" => $insurance_force_code,
            "`maintain_code`" => $maintain_code,
            "`maintain_date`" => $maintain_date,
            "`code`" => $code,
            "`insurance_business_code`" => $insurance_business_code,
            "`insurance_business_date`" => $insurance_business_date,
            "`taxi_license_code`" => $taxi_license_code,
            "`taxi_license_date`" => $taxi_license_date,
            "`remind_id`" => $remind_id,
            "`remind_day`" => $remind_day,
            "`remark`" => $remark,
            "`user_id`" => $_SESSION['userinfo']['id'],
            "`createtime`" => date("Y-m-d H:i:s")
        );

        $ID = $this->CarTable->add($arr, true);
        echo $ID;
        exit;
    }

    public function vehicleupdateAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        
        $id = $this->getParam('id');
        $name = $this->getParam('name');
        $team_id = $this->getParam('team_id');
        $code = $this->getParam('code');
        $mode = $this->getParam('mode');
        $driver_id = $this->getParam('driver_id');
        $engineer = $this->getParam('engineer');
        $head_weight = $this->getParam('head_weight');
        $load_weight = $this->getParam('load_weight');
        $ciq_code = $this->getParam('ciq_code');
        $clearance_card = $this->getParam('clearance_card');
        $ic_card = $this->getParam('ic_card');
        $electronics_clock = $this->getParam('electronics_clock');
        $bussiness_mode = $this->getParam('bussiness_mode');
        $driving_license_code = $this->getParam('driving_license_code');
        $driving_license_date = $this->getParam('driving_license_date');
        $insurance_force_date = $this->getParam('insurance_force_date');
        $insurance_force_code = $this->getParam('insurance_force_code');
        $maintain_code = $this->getParam('maintain_code');
        $maintain_date = $this->getParam('maintain_date');
        $insurance_business_code = $this->getParam('insurance_business_code');
        $insurance_business_date = $this->getParam('insurance_business_date');
        $taxi_license_code = $this->getParam('taxi_license_code');
        $taxi_license_date = $this->getParam('taxi_license_date');
        $remind_id = $this->getParam('remind_id');
        $remind_day = $this->getParam('remind_day');
        $remark = $this->getParam('remark');
        $unqiue = $this->CarTable->getRow("`name` = '{$name}' AND `status` > 0");
        $slim = $this->CarTable->getRow($id);
        if ($unqiue && $slim['name'] != $name) {
            echo -1;
            exit;
        }
        $arr = array(
            "`name`" => $name,
            "`team_id`" => $team_id,
            "`mode`" => $mode,
            "`driver_id`" => $driver_id,
            "`engineer`" => $engineer,
            "`head_weight`" => $head_weight,
            "`load_weight`" => $load_weight,
            "`ciq_code`" => $ciq_code,
            "`clearance_card`" => $clearance_card,
            "`ic_card`" => $ic_card,
            "`electronics_clock`" => $electronics_clock,
            "`bussiness_mode`" => $bussiness_mode,
            "`driving_license_code`" => $driving_license_code,
            "`driving_license_date`" => $driving_license_date,
            "`insurance_force_date`" => $insurance_force_date,
            "`insurance_force_code`" => $insurance_force_code,
            "`maintain_code`" => $maintain_code,
            "`maintain_date`" => $maintain_date,
            "`code`" => $code,
            "`insurance_business_code`" => $insurance_business_code,
            "`insurance_business_date`" => $insurance_business_date,
            "`taxi_license_code`" => $taxi_license_code,
            "`taxi_license_date`" => $taxi_license_date,
            "`remind_id`" => $remind_id,
            "`remind_day`" => $remind_day,
            "`remark`" => $remark
        );
        $this->CarTable->update($arr, $id);
        echo $id;
        exit;
    }

    public function vehiclejsonAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        
        $id = $this->getParam("id");
        $data = $this->CarTable->getRow($id);
        echo json_encode($data);
        exit;
    }

    public function vehicledeleteAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        
        $id = $this->getIntParam('id');
        $this->CarTable->update(array("`status`" => 0), $id);
        echo $id;
        exit;
    }

    public function vehicledormancyAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        
        $id = $this->getIntParam('id');
        $this->CarTable->update(array("`status`" => 2), $id);
        echo $id;
        exit;
    }

    public function vehicleactivationAction() {
        $uid = $_SESSION['userinfo']['id'];
        if (check_session_id($uid) == false) {
            unset($_SESSION['userinfo']);
            unlink('./sessionid/' . $uid . '.txt');
            $this->redirect("./?m=login&a=login");
        }
        
        $id = $this->getIntParam('id');
        $this->CarTable->update(array("`status`" => 1), $id);
        echo $id;
        exit;
    }

    protected function out() {
        $this->layout->nav = 'index';
        parent::out();
    }

}

Config::extend('carserviceController', 'Controller');
